#include "dialog.h"
#include "ui_dialog.h"

Dialog::Dialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::Dialog)
{
    ui->setupUi(this);

    devIndex = 0;
//    m_pRsuCtrl = RsuCtrlFactory::getDev(0);
    RsuMgr::getInstance()->setDevType(1);

    connect(RsuMgr::getInstance(), &RsuMgr::newFrameArrived,
            this, &Dialog::processRsuFrame);
    connect(RsuMgr::getInstance(), &RsuMgr::rsuInited,
            this, &Dialog::processRsuInitRlt);
    connect(RsuMgr::getInstance(), &RsuMgr::rsuInfoGot,
            this, &Dialog::processRsuInfoRlt);
    connect(RsuMgr::getInstance(), &RsuMgr::obuDetected,
            this, &Dialog::processNewObu);

//    connect(m_pRsuCtrl, &RsuCtrl::newFrameArrived,
//            this, &Dialog::processRsuFrame);
//    connect(m_pRsuCtrl, &RsuCtrl::rsuInited,
//            this, &Dialog::processRsuInitRlt);

//    connect(m_pRsuCtrl, &RsuCtrl::rsuInfoGot,
//            this, &Dialog::processRsuInfoRlt);
//    connect(m_pRsuCtrl, &RsuCtrl::obuDetected,
//            this, &Dialog::processNewObu);
}

Dialog::~Dialog()
{
    delete ui;
}

void Dialog::on_btnOpenRsu_clicked()
{
    QString sIp=ui->cmbRsuIp->currentText();
    int nPort=ui->cmbRsuPort->currentText().toInt();
//    m_pRsuCtrl->configRsuType(ui->cmbRsuType->currentText());
//    m_pRsuCtrl->openRsu(sIp, nPort);

    //
    RsuMgr::getInstance()->openRsu(1,sIp,nPort);
}

void Dialog::on_btnCloseRsu_clicked()
{
    if(devIndex == 0)
        devIndex = 1;
    else
        devIndex = 0;
//    m_pRsuCtrl->closeRsu();
}

void Dialog::on_btnReopenRsu_clicked()
{
//    m_pRsuCtrl->tryReconnect();
    RsuMgr::getInstance()->tryReconnect(1);
}

void Dialog::on_btnInitRsu_clicked()
{/*
    m_pRsuCtrl->initRsu(
            ui->spinBoxBstInterval->value(),
            ui->spinBoxRsuPower->value(),
            ui->spinBoxRsuChannel->value());*/
    RsuMgr::getInstance()->initRsu(1,
                ui->spinBoxBstInterval->value(),
                ui->spinBoxRsuPower->value(),
                ui->spinBoxRsuChannel->value());
}

void Dialog::on_btnDetectObu_clicked()
{
//    m_pRsuCtrl->detectObu();
//    m_pRsuCtrl->startDetectObu(50);
//    m_pRsuCtrl->setBeaconChangeInterval(2000);
    RsuMgr::getInstance()->startDetectObu(1,50);
}

void Dialog::processRsuFrame(int index,quint8 nFrameIndex, quint8 nFrameType, const QByteArray &frame)
{
    if(devIndex != index)
        return;
    QString sMsg;
    sMsg=tr("%1-%2-%3")
            .arg(QString::number(nFrameIndex, 16))
            .arg(QString::number(nFrameType, 16))
            .arg(frame.toHex().data());
    addMessage(sMsg);
}

void Dialog::processRsuInitRlt(int index,int nRsuStatus, const QByteArray &baRsuInfo)
{
    if(devIndex != index)
        return;
    QString sMsg;
    sMsg=tr("rsuStatus[%1] rsuInfo[%2]")
            .arg(nRsuStatus)
            .arg(baRsuInfo.toHex().data());
    addMessage(sMsg);
}

void Dialog::processPsamResetRlt(int index,int nPsamStatus, const QByteArray &baPsamInfo)
{
    if(devIndex != index)
        return;
    QString sMsg;
    sMsg=tr("psamStatus[%1] psamInfo[%2]")
            .arg(nPsamStatus)
            .arg(baPsamInfo.toHex().data());
    addMessage(sMsg);
}


void Dialog::processPsamTrannelRlt(int index,int status, int nRltCount, const QByteArray &baCmds)
{
    if(devIndex != index)
        return;
    QString sMsg;
    sMsg=tr("psamTrannelStatus[%1] nRltCount[%2] Rlt[%3]")
            .arg(status)
            .arg(nRltCount)
            .arg(baCmds.toHex().data());
    addMessage(sMsg);
}

void Dialog::processSecureFileRlt(int index,const QByteArray &baSecureFile,
                                  const QByteArray &baAuthenticator, int nStatus)
{
    if(devIndex != index)
        return;
    QString sMsg;
    sMsg=tr("processSecureFileRlt[%1] baSecureFile[%2] baAuthenticator[%3]")
            .arg(nStatus)
            .arg(baSecureFile.toHex().data())
            .arg(baAuthenticator.toHex().data());
    addMessage(sMsg);
}

void Dialog::processRsuInfoRlt(int index,const QByteArray &baRsuInfo)
{
    if(devIndex != index)
        return;
    QString sMsg;
    sMsg=tr("rsuInfo[%2]")
            .arg(baRsuInfo.toHex().data());
    addMessage(sMsg);
}

//void Dialog::processNewObu(int index,ObuInfo newObu, IccInfo newIcc)
void Dialog::processNewObu(int index,ObuInfo newObu)
{
//    Q_UNUSED(newIcc);

    if(devIndex != index)
        return;

    QString sMsg;
    sMsg=tr("obu[%1] detected. veh[%2].")
            .arg(newObu.getContractSN())
            .arg(newObu.getVehicleLicencePlateColorAndNumber());
    addMessage(sMsg);
}

void Dialog::addMessage(const QString &msg)
{
    const int MAX_BYTES = 20000;
    QString old = this->ui->plainTextEdit->toPlainText();
    if (old.length()>MAX_BYTES)
    {
        old = old.left(MAX_BYTES);
    }
    QString sTime = QTime::currentTime().toString("mm:ss.zzz");
    this->ui->plainTextEdit->setPlainText(sTime + ": " + msg + "\n" + old);
}

void Dialog::on_btnCheckRsuStatus_clicked()
{
//    m_pRsuCtrl->configRsuHeartBeatCheckStyle(ui->spinBoxRsuStatusCheckStyle->value());
//    m_pRsuCtrl->configHeartBeatCheckInterval(10);
//    m_pRsuCtrl->setConfigHeartBeat(ui->spinBoxRsuStatusCheckStyle->value(),10);
//    m_pRsuCtrl->checkRsuStatus();
    RsuMgr::getInstance()->checkRsuStatus(1);
}

void Dialog::on_btnGetRsuInfo_clicked()
{
//    m_pRsuCtrl->getRsuInfo();
    RsuMgr::getInstance()->getRsuInfo(1);
}

void Dialog::on_btnMockRawFrame_clicked()
{
    QString sHexData=this->ui->editRawFrame->text();
    QByteArray baData=QByteArray::fromHex(sHexData.toLocal8Bit());
//    m_pRsuCtrl->mockRawFrameData(baData);
}

void Dialog::on_btnReleaseObu_clicked()
{
//    m_pRsuCtrl->releaseObu();
}
